package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.annotion.ActionExecOrder;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.workflow.model.BpmFlowUserHistory;
import com.ccp.dev.workflow.service.BpmFlowUserHistoryService;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * ClassDescribe:
 * 指定执行人历史控制器类
 * @author :wangcheng
 * Date: 2019-07-24
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_WORKFLOW + "/bpmFlowUserHistory")
@Action(ownermodel = SysAuditModelType.PROCESS_MANAGEMENT)
public class BpmFlowUserHistoryController extends SysBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BpmFlowUserHistoryController.class);

    @Resource
    private BpmFlowUserHistoryService bpmFlowUserHistoryService;

    /**
     * 删除流程添加执行人历史记录
     * @param id 执行人历史记录id
     * @return 结果封装
     */
    @RequestMapping("/deleteById")
    @Action(description = "删除流程添加执行人历史记录", execOrder = ActionExecOrder.BEFORE, detail = "删除流程添加执行人历史记录")
    @ResponseBody
    public ResultData deleteById(@RequestParam String id) {
        try {
            bpmFlowUserHistoryService.delById(id);
            return getResultDataSuccess("delete.success");
        } catch (Exception e) {
            LOGGER.info(e.getMessage());
            return getResultDataError("delete.failure");
        }
    }

    /**
     * 添加流程指定执行人历史记录
     * @param procDefId 流程定义id
     * @param userHistory 执行人字符串
     * @return 封装结果
     */
    @RequestMapping("/add")
    @Action(description = "添加流程指定执行人历史记录", detail = "添加流程指定执行人历史记录")
    @ResponseBody
    public ResultData add(@RequestParam String procDefId, @RequestParam String userHistory) {
        userHistory = StringEscapeUtils.unescapeHtml4(userHistory);
        if (StringUtil.isEmpty(userHistory)) {
            return getResultDataError("save.failure");
        }
        String historyId = UUIDUtils.getUUIDFor32();
        BpmFlowUserHistory bpmFlowUserHistory = new BpmFlowUserHistory();
        bpmFlowUserHistory.setId(historyId);
        bpmFlowUserHistory.setFlowId(procDefId);
        String currentUserId = ContextUtil.getCurrentUserId();
        bpmFlowUserHistory.setUserId(currentUserId);
        bpmFlowUserHistory.setUserHistory(userHistory);
        bpmFlowUserHistory.setCreateTime(new Date());
        bpmFlowUserHistoryService.add(bpmFlowUserHistory);
        return getResultDataSuccess("save.success");
    }
}
